Repository navigation
Retain newlines in chat template when continue_final_message=True - #34253
Conversation
|
CI seems to be failing for reasons unrelated to this PR |
|
Wow, this issue was only introduced extremely recently, in #34214 ! I completely didn't foresee this being an issue when I merged that one. The fundamental problem is that some templates Maybe the right solution is to first try to match the input with trailing whitespace, and then fallback to searching for the stripped version if that fails? |
I like this approach - let me amend my PR. Do we also need to retain the |
|
No, I don't think so! I think it would break your task if we did, and I really just included it because I didn't realize the trailing whitespace could be meaningful. |
|
cc @schoennenbeck, who wrote #34214 - you don't need to do anything here, just making you aware that we might change the behaviour again! |
|
@Rocketknight1 I've added a try/except in 0fd98e1 I've also tested that the Llama code still works # From: https://github.com/huggingface/transformers/pull/34214
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4")
messages=[
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "Great question! The capital of France is "}
]
s = tokenizer.apply_chat_template(
messages, add_generation_prompt=False,
continue_final_message=True,
tokenize=False
)
print(s)Would you like me to include this as a regression test? I'm also not sure if we need something for the Qwen model, but for that we'd want to run it on GPU |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
|
@lewtun yeah, this feels like an area where other changes could easily break things again. A regression test would definitely be appreciated! |
| ) | ||
|
|
||
| @require_jinja | ||
| def test_continue_final_message_with_trim(self): |
Rocketknight1
left a comment
There was a problem hiding this comment.
This looks like a great PR to me, and the regression test is nice! I left two nits, but both are just code style rather than anything that actually affects the output.
| # Assert that the final message is unterminated | ||
| self.assertEqual( | ||
| prefill_output, | ||
| "<|im_start|>system\nsystem message<|im_end|>\n<|im_start|>user\nuser message<|im_end|>\n<|im_start|>assistant\nassistant message", |
There was a problem hiding this comment.
Nice! This is a clean test.
Co-authored-by: Matt <Rocketknight1@users.noreply.github.com>
|
@Rocketknight1 I've included your suggestions and the CI seems to be failing for reasons unrelated to this PR 😢 . What do you recommend doing in this case? |
|
@lewtun CI looks green now, so I'm happy! cc @ArthurZucker / @LysandreJik for core maintainer review and we should be good to go. |
LysandreJik
left a comment
There was a problem hiding this comment.
Thanks @lewtun for the PR and @Rocketknight1 for the review!
…uggingface#34253) * Retain newlines in chat template when * Add try/except * Add regression test * Simplify test * Apply suggestions from code review Co-authored-by: Matt <Rocketknight1@users.noreply.github.com> --------- Co-authored-by: Matt <Rocketknight1@users.noreply.github.com>
What does this PR do?
This PR addresses an issue with chat templates producing incorrect inputs when
continue_final_message=Trueand we wish to terminate generation on newlines like\n(as is common in process supervision with math reward models like this one).In this case, the prefills had their newlines stripped at each step of generation and produced results that disagree with pure greedy decoding to the EOS token. Here's an example:
Here's the reference completion from greedy decoding until EOS (note the new lines):
Without the fix we get bad generations because the prefill is continually stripping the trailing new lines per iteration:
With the proposed fix, order is restored and the resulting completion is sane:
Questions
strip_prefillarg inapply_chat_templatethat isTrueby default but can be disabled in cases like process supervision?Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.